﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Sumary
{

    public partial class FrmInfoRoom : XtraForm
    {
        readonly BLL.RoomBLL _bll = new BLL.RoomBLL();
        readonly DAL.tb_Room _roo = new DAL.tb_Room();
        readonly BLL.LeaseTypeBLL _typ = new BLL.LeaseTypeBLL();
        readonly BLL.GuestBLL _gue = new BLL.GuestBLL();
        readonly BLL.FeeBLL _fee = new BLL.FeeBLL();

        long _idLease = 0;

        /// <summary>
        /// Reload data when close this form
        /// </summary>
        public event MyDelegate MyEvent;
        bool IsClose
        {
            set
            {
                if (value)
                {
                    if (MyEvent != null) MyEvent();
                }
            }
        }

        public FrmInfoRoom()
        {
            InitializeComponent();
        }

        public FrmInfoRoom(long id)
            : this()
        {

            _roo = (DAL.tb_Room)_bll.GetByKey(id);
            Text = "Thông tin phòng: " + _roo.Name;

            lstInfoRoom.Items.Add(_roo.Name);
            lstInfoRoom.Items.Add("Loại phòng: " + _roo.tb_RoomType.Name);
            lstInfoRoom.Items.Add("Tình trạng: " + _roo.tb_Status.Name);
            lstInfoRoom.Items.Add("Sức chứa: " + _roo.Capacity);
            lstInfoRoom.Items.Add("Số giường: " + _roo.Numbeds);
            lstInfoRoom.Items.Add("Mô tả: " + _roo.Descript);
        }

        private void FrmInfoRoom_Load(object sender, EventArgs e)
        {
            lokLeaseTypeId.Properties.DataSource = _typ.Select();
            lokLeaseTypeId.ItemIndex = 0; // none

            if (_roo.StatusId == 2 || _roo.StatusId == 4) // used or book
            {
                var bll = new BLL.LeaseBLL();
                var lea = (DAL.tb_Lease)bll.GetByKey(_roo.Id);

                if (lea != null)
                {
                    _idLease = lea.Id;

                    lstInfoRoom.Items.Add("Khách: " + lea.tb_Guest.Name);
                    lstInfoRoom.Items.Add("CMND: " + lea.tb_Guest.IdCard);
                    lstInfoRoom.Items.Add("Ngày thuê: " + lea.ComingDate);
                    lstInfoRoom.Items.Add("Hình thức: " + lea.tb_LeaseType.Name);
                }
            }

            // Disable button Book & Lease
            if (_roo.tb_Status.Name == "Trống") // empty
            {
                cmdBook.Enabled = true;
                lokLeaseTypeId.Enabled = true;
                txtIdCard.Enabled = true;
            }
            else if (_roo.StatusId == 4) // booked
            {
                cmdBook.Enabled = false;
                lokLeaseTypeId.Enabled = false;
                txtIdCard.Enabled = false;

                cmdLease.Enabled = true;
            }
            else // used
            {
                cmdBook.Enabled = false;
                cmdLease.Enabled = false;
                cmdReceipt.Enabled = true;
            }
        }

        private void FrmInfoRoom_SizeChanged(object sender, EventArgs e)
        {
            gctMain.Left = (ClientSize.Width - gctMain.Width) / 2;
            gctMain.Top = (ClientSize.Height - gctMain.Height) / 2;
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cmdBook_Click(object sender, EventArgs e)
        {
            if ((txtIdCard.Text + "").Length != 9)
            {
                UTL.Message.Show("Chứ minh thư nhập 09 số", "Tìm kiếm");
                txtIdCard.Focus();
                return;
            }

            var guest = (DAL.tb_Guest)_gue.GetByKey(txtIdCard.Text);
            if (guest != null)
            {
                var lea = new BLL.LeaseBLL();

                var o = new DAL.tb_Lease()
                {
                    GuestId = guest.Id,
                    RoomId = _roo.Id,
                    LeaseTypeId = UTL.Number.ToInt64(lokLeaseTypeId.EditValue + ""),
                    UserId = FrmMain._sss.Id,

                    ComingDate = DAL.BaseDAL.CurrentTime.Value,
                    PriceElec = _fee.PriceElec,
                    PriceWater = _fee.PriceWater
                };

                var oki = lea.Insert(o);
                if (oki == UTL.Session.PerState.Duplicate)
                    UTL.Message.Show("Đã đặt rồi", "Đặt phòng");
                if (oki == UTL.Session.PerState.Success)
                {
                    if (_bll.Book(_roo.Id))
                    {
                        Close();
                        UTL.Message.Show("Đặt phòng thành công!", "Đặt phòng");
                    }
                }
            }
            else
            {
                foreach (var x in MdiChildren) if (x is Catalog.FrmGuest) return;
                var frm = new Catalog.FrmGuest();
                frm.MdiParent = MdiParent;
                frm.Show();
            }
        }

        private void cmdLease_Click(object sender, EventArgs e)
        {
            if (_bll.Lease(_roo.Id)) Close();
        }

        private void cmdReceipt_Click(object sender, EventArgs e)
        {
            var bll = new BLL.ReceiptBLL();
            if (bll.Insert(_idLease) == UTL.Session.PerState.Success)
            {
                Close();
            }
            else
            {
                UTL.Message.Show("Không thanh toán được,\nchưa cập nhật số điện, nước", "Thanh toán");
            }
        }

        private void FrmInfoRoom_FormClosed(object sender, FormClosedEventArgs e)
        {
            IsClose = true;
        }

        private void txtIdCard_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) cmdBook_Click(null, null);
        }
    }
}